package com.example.chenhaosblog.controller;

import com.example.chenhaosblog.bean.User;
import com.example.chenhaosblog.service.IUserService;
import com.example.chenhaosblog.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class UserController {
    @Autowired
    private IUserService service;

 /*   @Autowired
    private RestTemplate restTemplate ;  //通过这个类调用其它模块的接口*/
    @RequestMapping("/userList")
    public List<User> getUserList(){


   /*     //调用其它boot的接口
        String url = "http://localhost:8087/test2/userList";
        User[] users =  restTemplate.getForObject(url,  User[] .class);
        List<User> userList = Arrays.asList(users);*/
        return service.findAllUser();
    }
    @RequestMapping("/add")
    public String addUser(){
        User user = new User();
        user.setUsername("chenhao");
        if(user!=null){
            service.createUser(user);
            return "success";
        }else{
            return "error";
        }
    }
    @RequestMapping("/add2")
    public String addUser2(){
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("username","cjhenhao");
        if(map!=null){
            service.createUser2(map);
            return "success";
        }else{
            return "error";
        }
    }


    @RequestMapping("/userList2")
    public PageData getUserLis2t() throws Exception {
        PageData pageData = new PageData();
        List<PageData> all = service.findAll(pageData);
        pageData.put("data",all);
        return pageData;
    }
}
